/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.app.Context;

import com.xuexiang.xui_lib.util.ViewModel;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.ability.SponsoredAbility;
import com.xuexiang.xuidemo.util.JumpUtil;
import com.xuexiang.xuidemo.util.ToastUtil;
import com.xuexiang.xuidemo.util.WebUtils;
import com.xuexiang.xuidemo.view.DoubleLineListItemFactory;

/**
 * 导航界面
 *
 * @since 2021-03-19
 */
public class Aboutabilityslice extends AbilitySlice {
    private String[] data;
    private Context context;
    private final int anInt2 = 2;
    private final int anInt3 = 3;
    private final int anInt4 = 4;
    private final int anInt5 = 5;

    /**
     * 加载compoent
     *
     * @param intent
     */
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_aboutslice_layout);
        context = this;
        ViewModel viewModel = new ViewModel(this);
        data = viewModel.getStringResource(ResourceTable.Strarray_title_array);
        initItems();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initItems() {
        DoubleLineListItemFactory doubleLineListItemFactory = new DoubleLineListItemFactory(getContext(), data);
        doubleLineListItemFactory.setCliklinlist(new DoubleLineListItemFactory.Cliklinlist() {
            @Override
            public void onclict(String page) {
                if (page.equals(data[0])) {
                    WebUtils.goWeb(context, "https://xuexiangjys.github.io/XUI/");
                } else if (page.equals(data[1])) {
                    WebUtils.goWeb(context, "https://github.com/xuexiangjys/XUI/wiki/");
                } else if (page.equals(data[anInt2])) {
                    WebUtils.goWeb(context, "https://github.com/xuexiangjys/XUI/");
                } else if (page.equals(data[anInt3])) {
                    ToastUtil.showToast(context, "已是最新版本");
                } else if (page.equals(data[anInt4])) {
                    JumpUtil.jumpToAbility(context, SponsoredAbility.class);
                } else if (page.equals(data[anInt5])) {
                    ToastUtil.showToast(context, "暂不支持跳转QQ");
                }
            }
        });
        DirectionalLayout aboutPageList = (DirectionalLayout) findComponentById(ResourceTable.Id_aboutPageLowerPart);
        aboutPageList.removeAllComponents();
        for (int i = 0; i < data.length - 1; i++) {
            aboutPageList.addComponent(doubleLineListItemFactory
                .getDoubleLineList(data[i], "无用参数"));
        }
        DirectionalLayout lastItem = doubleLineListItemFactory
            .getDoubleLineList("加入QQ官方交流群", "无用参数");
        lastItem.findComponentById(ResourceTable.Id_divider).setVisibility(Component.INVISIBLE);
        aboutPageList.addComponent(lastItem);
    }
}
